package com.base.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.base.comm.CommUrls;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.base.comm.CommContent;
import com.base.po.voip.Callnums;
import com.base.service.CallNumService;
import com.base.service.LogsService;
import com.base.utils.IP;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * 添加修改回拨号码信息服务端
 * 
 * @author f 优化日期：10-16
 *
 */
@Controller
@RequestMapping("/admin")
public class DialUpdateController {

	@Autowired
	private LogsService logsService; // 日志Service
	@Autowired
	private CallNumService callNumService; // 回拨号码Service

	// 修改回拨号码
	@RequestMapping(value = "/dial_do_update", method = RequestMethod.POST)
	public @ResponseBody int doUpdate(HttpServletRequest request, Callnums call) throws Exception {
		HttpSession session = request.getSession(false); // 获取session
		if (session == null) {
			return -1;
		}
		Object obj_id = session.getAttribute("adminUserId"); // 登录ID
		Object obj_name = session.getAttribute("userNick"); // 登录名称
		if (obj_id == null || obj_name == null) {
			return -1;
		}
		int flag; // 成功状态
		if (obj_id.equals("7dd9a07e-9cef-4702-a165-b7033861c1b7")){
			flag = admin_update(request, call);
		}else{
			if (callNumService.selectByPrimaryKey(call.getId()) == null) {
				call.setAgentid((String) obj_id); // 设置参数 代理商ID
				flag = callNumService.insert(call); // 新增
			} else {
				flag = callNumService.updateByPrimaryKeySelective(call); // 修改
			}
		}

		logsService.insert(CommContent.TYPE_UPDATE, CommContent.LOGS_UPDATE + "改动回拨号码信息", (String) obj_name, true,
				IP.getIpAddr(request));
		return flag;
	}

	@SuppressWarnings("resource")
	public int admin_update(HttpServletRequest request, Callnums call) throws Exception {
		// 查询admin 是否存在回拨号码
		Callnums adminNums = callNumService.selectByAgentKey("7dd9a07e-9cef-4702-a165-b7033861c1b7");
		if (adminNums == null){
			call.setAgentid("7dd9a07e-9cef-4702-a165-b7033861c1b7");
			callNumService.insertSelective(call);
		}

		// 批量修改回拨号码
		Connection conn = null;
		PreparedStatement pstm = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(CommUrls.JDBC, CommUrls.JDBC_USER, CommUrls.JDBC_PASSWORD);

			if(call != null) {
				String sql = "update callnums set Nums = ?";
				pstm = conn.prepareStatement(sql);
				pstm.setString(1, call.getNums());
				pstm.executeUpdate();
			}

		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		} finally {
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					e.printStackTrace();
					throw new RuntimeException(e);
				}
			}
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
					throw new RuntimeException(e);
				}
			}
		}

		return 1;
	}
}
